<%@ page import="net.sourceforge.wurfl.core.*" %>
<%@ page import="java.util.Enumeration" %>
<%@ page import="org.apache.log4j.Logger" %>
<%! static final Logger log = Logger.getLogger("CAPS.JSP"); %>

<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Home</title>            
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link rel="shortcut icon" href="/ufone/images/favicon.ico" />
        <link rel="stylesheet" type="text/css" href="/ufone/css/black/style.css" />        
        <style type="text/css">ul{list-style-image:url("/ufone/images/blue-bullet.gif"); padding-left:10px; margin-left:2px;}</style>
    </head>
    <body style="font-size: 75%; margin: 0px; padding: 0px; font-family: Tahoma,Verdana; ">        

<%
    
    String ua = "";
    String preferred_markup = "";
    String callTag = "";
    String smsTag = "";
    String nxApn1 = "";
    String nxApn2 = "";
    String msisdn = "";
    String strBrand = "";
    String strModel = "";
    String ipAdd = "";
    int max_image_width = 0;
    int resolution_width = 0;
    
    boolean isXhtml = true;
    boolean isHtml = true;
    boolean isWml = true;
    boolean isXTable = true;
    boolean isWTable = true;
    boolean isTableSupport = true;
    
    try {
    	try { 
    		if( (ua = request.getParameter("ua")) != null && (ua.length() > 2) ) {  
    		}
    		else {
        ua = request.getHeader( "User-Agent" );
    			if(ua.startsWith("Opera")) {
    				String tempUA = request.getHeader("x-operamini-phone-ua");
    				if(tempUA != null) {
    					ua = tempUA;
    				}				
    				else if((tempUA = request.getHeader("x-operette-ua")) != null){
    					ua = tempUA;
    				}
    			}
    		}
    	} catch (Exception ex) { 
    		ua = ""; 
    	}
    	WURFLHolder wurflHolder = (WURFLHolder) getServletContext().getAttribute("net.sourceforge.wurfl.core.WURFLHolder");
    	WURFLManager wurfl = wurflHolder.getWURFLManager();
    	Device device = wurfl.getDeviceForRequest(request);
        
        strBrand = device.getCapability("brand_name");
        strModel = device.getCapability("model_name");
        
        max_image_width = Integer.parseInt(device.getCapability("max_image_width"));
        resolution_width = Integer.parseInt(device.getCapability("resolution_width"));
        
        callTag = device.getCapability("xhtml_make_phone_call_string");
        smsTag = device.getCapability("xhtml_send_sms_string");
        
        preferred_markup = device.getCapability("preferred_markup");
        isXhtml = preferred_markup.toLowerCase().contains("xhtml");
        isHtml = preferred_markup.toLowerCase().startsWith("html_web");
        isWml = preferred_markup.toLowerCase().contains("wml");
        isXTable = device.getCapability("xhtml_table_support").equalsIgnoreCase("true");
        isWTable = device.getCapability("table_support").equalsIgnoreCase("true");
        
        //nxApn1 = request.getHeader("X-Nx-Apn");
        msisdn = request.getHeader("x-msisdn");
        nxApn2 = request.getHeader("x-nx-apn");
        ipAdd = (String)request.getRemoteHost();
        
        
        isTableSupport = ( isHtml || (isXhtml && isXTable) );
    }
    catch( Exception e ) {
        log.debug("Exception querying WURFL for: "+ ua );
    }
    
    String headerName = "";
    String header = "";
    log.debug("     ----------------------------");
    log.debug(" Header Information");
    Enumeration headerNames = request.getHeaderNames();
    %>
    <table>
    <tr><th colspan="2">Header Information</th></tr>
    <%
    while (headerNames.hasMoreElements()) {
        headerName = (String) headerNames.nextElement();
        header =(String)request.getHeader(headerName);
        log.debug(headerName +"\t: "+ header);
        %>
        <tr>
            <td><%=headerName%></td>
            <td><%=header%></td>
        </tr>
        <%
    }
    %>
    <tr>
        <td>Remote Host</td>
        <td><%=ipAdd%></td>
    </tr>
    <tr>
		<td>Images Path</td>
        <td><%=getServletContext().getRealPath("/images/")%></td>
    </tr>
    <tr>
        <td colspan="2"> -------------------------------------- </td>
    </tr>    
    </table>
    <%
    
    log.debug("IP               : "+ ipAdd);
    log.debug("     ----------------------------");
    log.debug(" HandSet Capabilities");
    log.debug("ua               : "+ ua);
    log.debug("Brand            : "+ strBrand);
    log.debug("Model            : "+ strModel);
    log.debug("max_image_width  : "+ max_image_width);
    log.debug("resolution_width : "+ resolution_width);
    log.debug("preferred_markup : "+ preferred_markup);
    log.debug("isXhtml          : "+ isXhtml);
    log.debug("isHtml           : "+ isHtml);
    log.debug("isWml            : "+ isWml);
    log.debug("isXTable         : "+ isXTable);
    log.debug("isWTable         : "+ isWTable);
    log.debug("isTableSupport   : "+ isTableSupport);
    log.debug("callTag          : "+ callTag);
    log.debug("smsTag           : "+ smsTag);
    log.debug("nxApn1           : "+ nxApn1);
    log.debug("nxApn2           : "+ nxApn2);
    log.debug("     ----------------------------");
    
%>

<table>
	<tr><th colspan="2">WURFL Information</th></tr>
    <tr>
        <td>User Agent</td>
        <td><%=ua%></td>
    </tr>
    <tr>
        <td>Brand</td>
        <td><%=strBrand%></td>
    </tr>
    <tr>
        <td>Model</td>
        <td><%=strModel%></td>
    </tr>
    <tr>
        <td>max_image_width</td>
        <td><%=max_image_width%></td>
    </tr>
    <tr>
        <td>resolution_width</td>
        <td><%=resolution_width%></td>
    </tr>
    <tr>
        <td>preferred_markup</td>
        <td><%=preferred_markup%></td>
    </tr>
    <tr>
        <td>isXhtml</td>
        <td><%=isXhtml%></td>
    </tr>
    <tr>
        <td>isHtml</td>
        <td><%=isHtml%></td>
    </tr>
    <tr>
        <td>isWml</td>
        <td><%=isWml%></td>
    </tr>
    <tr>
        <td>isXTable</td>
        <td><%=isXTable%></td>
    </tr>
    <tr>
        <td>isWTable</td>
        <td><%=isWTable%></td>
    </tr>
    <tr>
        <td>isTableSupport</td>
        <td><%=isTableSupport%></td>
    </tr>
    <tr>
        <td>callTag</td>
        <td><%=callTag%></td>
    </tr>
    <tr>
        <td>smsTag</td>
        <td><%=smsTag%></td>
    </tr>
    <tr>
        <td>nxApn1</td>
        <td><%=nxApn1%></td>
    </tr>
    <tr>
        <td>nxApn2</td>
        <td><%=nxApn2%></td>
    </tr>
</table>


</body>
</html>